package com.wyz.sys.controller;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.wyz.domain.User;
/**  
 * @Title UserController.java  
 * @description TODO  
 * @time 2017年3月15日 上午11:34:31  
 * @author wyz  
 * @version 1.0  
 **/
@Controller
@RequestMapping(value="/sys")
public class UserController {
	
	private static List<User> userList;
	
	public UserController() {
		super();
		userList = new ArrayList<User>();
	}
	
	private static final Log logger = LogFactory
			.getLog(UserController.class);
	
	@RequestMapping(value="/register", method=RequestMethod.GET)
	public String registerForm() {
		logger.info("register GET方法被调用...");
		
		return "registerForm";
	}
	
	// 该方法映射的请求为http://localhost:8080/myc04/user/register
	@RequestMapping(value="/registersec",method=RequestMethod.POST)
	// 将请求中的loginname参数的值赋给loginname变量,password和username同样处理
	public String register(
			 @RequestParam("loginname") String loginname,
			 @RequestParam("password") String password,
			 @RequestParam("username") String username) {
		 logger.info("register POST方法被调用...");
		 // 创建User对象
		 
		 System.out.println("\n\nregistersec POST方法被调用...");
		 System.out.println("获取客户提交的FORM==loginname: " + loginname
				 + ", password: " + password
				 + ", username: " + username);
		 
		 User user = new User();
		 user.setLoginname(loginname);
		 user.setPassword(password);
		 user.setUsername(username);
		 // 模拟数据库存储User信息
		 userList.add(user);
		 // 跳转到登录页面
	     return "loginForm";
	}
	 
	// 该方法映射的请求为http://localhost:8080/myc04/user/login
	@RequestMapping("/login")
	public String login(
			// 将请求中的loginname参数的值赋给loginname变量,password同样处理
			 @RequestParam("loginname") String loginname,
			 @RequestParam("password") String password,
			 Model model) {
		 logger.info("登录名:"+loginname + " 密码:" + password);
		 // 到集合中查找用户是否存在，此处用来模拟数据库验证
		 for(User user : userList){
			 if(user.getLoginname().equals(loginname) 
					 && user.getPassword().equals(password)){
				 model.addAttribute("user",user);
				 return "welcome";
			 }
		 }
	     return "loginForm";
	}

}
 